/** 打开tab选项卡 */
function openTab(icon,menuName,url,tabId) {
	var element = layui.element;
	var eachcount=0;
	var flag= false;
	$(".content-tab-title").find('li').each(function() {
		eachcount++;
		var layId = $(this).attr("lay-id");
		if(tabId == layId){
			flag = true;
		}
		if(eachcount >= $(".content-tab-title").find('li').length){
			if(flag){
				element.tabChange('tabNav', tabId);
				return;
			}else{
				//添加tab
				element.tabAdd('tabNav', {
					title: "<i class='fa fa-"+icon+"' style='font-size: 16px;'></i>&nbsp;"+menuName,
					content: "<iframe src='"+url+"' scrolling='auto' width='100%' height='100%' frameborder='0' allowfullscreen='true' webkitallowfullscreen='true' mozallowfullscreen='true' class='layui-anim layui-anim-upbit perfree-ifram'></iframe>", //支持传入html
					id: tabId
				});
				//切换至新添加tab
				element.tabChange('tabNav', tabId);
				util.iframeAuto();
				if(util.getWidth() < 640){
					hideNav();
					flag = 0;
				}
			}
		}
	});
}
/** 收起侧边栏 */
var flag = 0;
$(".menuBtn").on("click",function(){
	if(util.getWidth() < 640){
		if(flag == 1){
			hideNav();
			flag = 0;
		}else{
			showNav();
			flag = 1;
		}
	}
	if(util.getWidth() >= 640){
		if(flag == 0){
			hideNav();
			flag = 1;
		}else{
			showNav();
			flag = 0;
		}
	}
});
/** 菜单导航点击事件 */
$('.left-menu').delegate('.nav-menu-a','click',function () {
	if(flag == 1){
		showNav();
		flag = 0;
	}
});
/** 隐藏导航栏文字,显示图标 */
function hideNav(){
	if(util.getWidth() < 640){
		$(".nav-menu-a > .menu-text,.nav-menu-a > .layui-nav-more").fadeOut(100);
		$(".left-navbar-menu,.layui-logo").animate({width:0}, 200);
		$(".logo-text > .logo-text-value").fadeOut(0);
		$(".layui-body,.layui-footer,.navbar-header").animate({left:0}, 200);
		$(".nav-header-right").animate({right:"0px"}, 200);
		$(".child-menu").css("display","none");
	}
	if(util.getWidth() >= 640){
		$(".nav-menu-a > .menu-text,.nav-menu-a > .layui-nav-more").fadeOut(100);
		$(".left-navbar-menu,.layui-logo").animate({width:50}, 200);
		$(".logo-text > .logo-text-value").fadeOut(0);
		$(".layui-body,.layui-footer,.navbar-header").animate({left:50}, 200);
		$(".nav-header-right").animate({right:"70px"}, 200);
		$(".child-menu").css("display","none");	// 原先
	}
}
/** 显示完全菜单导航 */
function showNav(){

	if(util.getWidth() < 640){
		layer.closeAll('tips');
		$(".layui-body,.layui-footer,.navbar-header").animate({left:"200px"}, 200 );
		$(".nav-header-right").animate({right:"170px"}, 200 );
		$(".logo-text > .logo-text-value,.nav-menu-a > .menu-text").delay(200).fadeIn(200);
		$(".layui-logo,.left-navbar-menu").animate({width:200}, 200 );
		$(".nav-menu-a > .layui-nav-more").delay(200).fadeIn(0);
		$(".child-menu").css("display","block");
	}
	if(util.getWidth() >= 640){
		layer.closeAll('tips');
		$(".layui-body,.layui-footer,.navbar-header").animate({left:"200px"}, 200 );
		$(".nav-header-right").animate({right:"220px"}, 200 );
		$(".logo-text > .logo-text-value,.nav-menu-a > .menu-text").delay(200).fadeIn(200);
		$(".layui-logo,.left-navbar-menu").animate({width:200}, 200 );
		$(".nav-menu-a > .layui-nav-more").delay(200).fadeIn(0);
		$(".child-menu").css("display","block");
	}
}
/** 菜单缩小时,鼠标移出隐藏tips */
$('.left-menu').on('mouseleave', '.layui-nav-item', function(){
	// layer.closeAll('tips');
    // $("#hideMenu").hide();
});

$('#hideMenu').on('mouseleave', function () {
	$(this).hide();
})
/** 菜单缩小时,鼠标悬浮显示tips */
$('.left-menu').on('mouseenter', '.layui-nav-item', function(e){
	if(flag == 1){
        if ($(this).children('.layui-nav-child').length > 0) {
            console.log(JSON.stringify($(this).children('.layui-nav-child').find('.nav-menu-a-child')));
            // var arr = $(this).children('.layui-nav-child').find('.nav-menu-a-child')
            // var str = ''
            // for (var i =0;i< arr.length; i++){
            //     console.log(arr[i], i);
            //     str += arr[i]
            // }
            // $("#hideMenu").append(str)
        } else {
        	console.log($(this).children(".nav-menu-a").children(".menu-text").text())
        }

		var catTop;
		var borderTop = $(this).offset().top - 3;
		var viewHeight = $(window).height();
		var scrollTop = $(document).scrollTop();
		var relaxHeight = viewHeight - (borderTop - scrollTop);
		var hideMenuheight = $("#hideMenu").height()
		if (hideMenuheight <= relaxHeight) {
            catTop = borderTop
		} else if (hideMenuheight > relaxHeight && hideMenuheight < viewHeight) {
            catTop = scrollTop + viewHeight - hideMenuheight - 10;
        } else {
            catTop = scrollTop + 5;
        }
        $("#hideMenu").show().css('top', catTop)

        // if ($(this).children('.layui-nav-child').length > 0) {
        //     console.log($(this).children('.layui-nav-child').find('.nav-menu-a-child').length);
		// } else {
		// 	console.log($(this).children(".nav-menu-a").children(".menu-text").text())
		// }
		// $("#hideMenu").show();

        // console.log( .children(".menu-text").text() )
		// var tip = $(this).children(".nav-menu-a").children(".menu-text").text();
		// var that = this;
		// layer.closeAll('tips');
		// layer.open({type: 4,shade: 0,closeBtn: 0,content: [tip, that]});
	}
});
/** 顶部导航鼠标悬浮显示tips */
$(".nav-top-icon").hover(
	function(){
		var tip = $(this).children("a").attr("lay-tips");
		var that = this;
		layer.closeAll('tips');
		layer.open({type: 4,tips: 3,shade: 0,closeBtn: 0,content: [tip, that]});
	},
	function(){
		layer.closeAll('tips');
	}
)
/**更换主题*/
 $('.choose-theme-btn').on('click', function () {
	 $(".theme").animate({right:0}, 200 );
	 util.coverShade();
});
/** 点击关闭遮罩及右侧主题页 */
function offShade() {
	$(".theme").animate({right:"-288px"}, 200 );
	 util.hideShade();
 }
/** 刷新 */
$('.refreshBtn').on('click', function () {
	var content = $(".layui-show >iframe");
	content.attr('src', content.attr('src'));
});
/**全屏*/
 $('.full-scann').on('click', function () {
	 var docElm = document.documentElement;
	if (docElm.requestFullscreen) {docElm.requestFullscreen();}
	else if (docElm.mozRequestFullScreen) {docElm.mozRequestFullScreen();}
	else if (docElm.webkitRequestFullScreen) {docElm.webkitRequestFullScreen();}
	else if (elem.msRequestFullscreen) {elem.msRequestFullscreen();}
	layer.msg('按Esc即可退出全屏');
});
//测试主题更换方法(未完成)
var cssArr="";
$('.theme-box-body').delegate('.theme-box','click',function () {
	if(cssArr == ""){
		var url = $(this).attr("css-url");
		cssArr = url;
		util.dynamicLoadCss(url);
	}else{
		util.removejscssfile(cssArr,"css");
		var url = $(this).attr("css-url");
		cssArr = url;
		util.dynamicLoadCss(url);
	}

});

/** 定义事件 **/
var admin = {
    // 滑动选项卡
    rollPage: function (d) {
        var $tabTitle = $('.layui-layout-admin .layui-body .layui-tab .layui-tab-title');
        var left = $tabTitle.scrollLeft();
        if ('left' === d) {
            $tabTitle.scrollLeft(left - 120);
        } else if ('auto' === d) {
            var autoLeft = 0;
            $tabTitle.children("li").each(function () {
                if ($(this).hasClass('layui-this')) {
                    return false;
                } else {
                    autoLeft += $(this).outerWidth();
                }
            });
            $tabTitle.scrollLeft(autoLeft - 47);
        } else {
            $tabTitle.scrollLeft(left + 120);
        }
    },
    // 关闭选项卡操作菜单
    closeTabOperNav: function () {
        $('.layui-icon-down .layui-nav .layui-nav-child').removeClass('layui-show');
    }
};

/** 关闭当前选项卡  **/
$('body').on('click', '.closeNowBtn', function () {
	admin.closeTabOperNav();
    var $title = $('.layui-layout-admin .layui-body .layui-tab .layui-tab-title');
    if ($title.find('li:eq(1)').hasClass('layui-this')) {
        layer.msg('主页不能关闭', {icon: 2});
        return;
    }
    $title.find('li.layui-this').find(".layui-tab-close").trigger("click");
});


/** 关闭其他选项卡 **/
$('body').on('click', '.closeOtherTabs', function () {
    admin.closeTabOperNav();
    $('.layui-layout-admin .layui-body .layui-tab .layui-tab-title li:gt(1):not(.layui-this)').find('.layui-tab-close').trigger('click');
});

/**  关闭全部选项卡 **/
$('body').on('click', '.closeAllTabs', function () {
    $('.layui-layout-admin .layui-body .layui-tab .layui-tab-title li:gt(1)').find('.layui-tab-close').trigger('click');
    $('.layui-layout-admin .layui-body .layui-tab .layui-tab-title li:eq(1)').trigger('click');
    admin.closeTabOperNav();
});

/** 左滑动 **/
$('body').on('click', '.leftPage', function () {
    admin.rollPage("left");
});

/** 右滑动 **/
$('body').on('click', '.rightPage', function () {
    admin.rollPage();
});

function treeMenu(node) {
	var linkDOM = ''
	for (var i = 0; i< node.length; i++){
		linkDOM += node[i]
	}
    return linkDOM
}
